package wysiwyg.shapes
{
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.binding.utils.BindingUtils;
	
	import wysiwyg.objectHandles.LineHandles;

	public class LineShape extends ShapeBase
	{
		private var origStartPoint:Point;
		private var origEndPoint:Point;
		private var lineHandles:LineHandles;
		public function LineShape()
		{
			super();
		}
		override protected function createChildren():void
		{
			shapeHandles=new LineHandles();
			parentLayer.addChild(shapeHandles);
			
			lineHandles=shapeHandles as LineHandles;
			lineHandles.associatedLine=this;
			
			BindingUtils.bindProperty(shapeHandles,"visible",this,"selected");
		}
		override protected function onMouseDown(event:MouseEvent):void
		{
			if(selected)
			{
				isHoldingShape=true;
				
				origStartPoint=new Point();
				origEndPoint=new Point();
				mouseDownPos=new Point();
				
				origStartPoint.x=lineHandles.startPoint.x;
				origStartPoint.y=lineHandles.startPoint.y;
				
				origEndPoint.x=lineHandles.endPoint.x;
				origEndPoint.y=lineHandles.endPoint.y;
				
				mouseDownPos.x=theModel.workspace.mouseX;
				mouseDownPos.y=theModel.workspace.mouseY;
				
				theModel.workspace.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
				theModel.workspace.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			}
		}
		override protected function onMouseMove(event:MouseEvent):void
		{
			if(isHoldingShape)
			{
				lineHandles.startPoint.x=origStartPoint.x+(theModel.workspace.mouseX-mouseDownPos.x);
				lineHandles.startPoint.y=origStartPoint.y+(theModel.workspace.mouseY-mouseDownPos.y);
				
				lineHandles.endPoint.x=origEndPoint.x+(theModel.workspace.mouseX-mouseDownPos.x);
				lineHandles.endPoint.y=origEndPoint.y+(theModel.workspace.mouseY-mouseDownPos.y);
				lineHandles.invalidateDisplayList();
				invalidateDisplayList();
			}
		}
		override protected function updateDisplayList(w:Number, h:Number):void
		{
			if(!(shapeHandles as LineHandles).endPoint||!(shapeHandles as LineHandles).startPoint)
				return;
			graphics.clear();
			graphics.lineStyle(lineThickness,lineColor,lineAlpha);
			graphics.moveTo((shapeHandles as LineHandles).startPoint.x,(shapeHandles as LineHandles).startPoint.y);
			graphics.lineTo((shapeHandles as LineHandles).endPoint.x, (shapeHandles as LineHandles).endPoint.y);
		}
	}
}